package com.example.zwh.computershop.classify;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.zwh.computershop.R;

import java.util.List;

public class ClassifyBtnAdapter extends RecyclerView.Adapter<ClassifyBtnAdapter.ViewHolder> {
    private List<Classify_btn> mBtnList;
    private OnItemClickLitener mOnItemClickLitener;

    static class ViewHolder extends RecyclerView.ViewHolder {
        Button btn_item;

        public ViewHolder (View view) {
            super(view);
            btn_item = view.findViewById(R.id.classify_item_btn);
        }
    }

    public ClassifyBtnAdapter(List<Classify_btn> mBtnList) {
        this.mBtnList = mBtnList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.classify_btn_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Classify_btn btn = mBtnList.get(position);
        holder.btn_item.setText(btn.getName());
        holder.btn_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = holder.getLayoutPosition();
                Classify_btn btn = mBtnList.get(pos);
                mOnItemClickLitener.onItemClick(mBtnList.get(pos).getName(), btn.getMark());
            }
        });
    }
    @Override
    public int getItemCount() {
        return mBtnList.size();
    }
    //起到传值效果
    public interface OnItemClickLitener {
        public void onItemClick(String name, int mark);
    }

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}
